package com.hotacorp.opencloud.flowservice.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.mybatisplus.Query;
import com.hotacorp.opencloud.flowservice.dao.LeaveRequestDao;
import com.hotacorp.opencloud.flowservice.entity.LeaveRequestEntity;
import com.hotacorp.opencloud.flowservice.service.LeaveRequestService;

/**
 * 请假申请接口实现
 * @author lwg
 *
 */
@Service("leaveRequestService")
public class LeaveRequestServiceImpl extends ServiceImpl<LeaveRequestDao, LeaveRequestEntity> implements LeaveRequestService {

    @Override
    public PageData queryPage(Map<String, Object> params) {
    	QueryWrapper<LeaveRequestEntity> query= new QueryWrapper<LeaveRequestEntity>();
    	query.eq("tenantid", params.get("tenantid"));
    	query.eq("username", params.get("username"));
		String timefrom = (String) params.get("timefrom");
		String timeto = (String) params.get("timeto");
		if (StringUtils.isNotEmpty(timefrom)) {
			try {
				Date from = DateUtils.parseDate(timefrom, "yyyy-MM-dd");
				query.ge("startdate", from);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (StringUtils.isNotEmpty(timeto)) {
			try {
				Date to = DateUtils.parseDate(timeto, "yyyy-MM-dd");
				query.le("startdate", to);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	query.orderByDesc("createtime");
        IPage<LeaveRequestEntity> page = this.page(
                new Query<LeaveRequestEntity>().getPage(params),
                query
        );
//        List<LeaveRequestInfo> list = new ArrayList<>();
//        page.getRecords().forEach(action->{
//        	LeaveRequestInfo jjj = new LeaveRequestInfo();
//        	BeanUtils.copyProperties(action, jjj);
//        	list.add(jjj);
//        });
        return new PageData(page);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean setProcessInstanceId(Long tenantid, String username, Long leaverequestid, String processinstanceid) {
		LeaveRequestEntity entity = this.baseMapper.selectById(leaverequestid);
		if (entity != null && entity.getTenantid().longValue() == tenantid.longValue() && StringUtils.equals(username, entity.getUsername())) {
			entity.setProcessinstanceid(processinstanceid);
			baseMapper.updateById(entity);
			return true;
		}
		return false;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Long insert(LeaveRequestEntity entity) {
		boolean ssss = super.save(entity);
		if (!ssss) throw new RuntimeException("提交申请错误！");
		return entity.getId();
	}


}